package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.List;

@Slf4j
@Service
@Component
public class IProductServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ProductConverter productConverter;

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());//TODO
            ProductDetailDto dto = productConverter.item2DetailDto(item);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            String[] images = item.getImages();
            dto.setProductImageSmall(Arrays.asList(images));
            dto.setDetail(itemDesc.getItemDesc());

            response.setProductDetailDto(dto);
        } catch (Exception e) {
            //e.printStackTrace();
            //response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            //response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
            log.error("com.mall.shopping.services.IProductServiceImpl Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();
        try {
            request.requestCheck();
            Example example = new Example(Item.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("cid", request.getCid());
            if (request.getPriceGt() != null && request.getPriceLte() == null) {
                criteria.andGreaterThanOrEqualTo("price", request.getPriceGt());
            }
            if (request.getPriceGt() == null && request.getPriceLte() != null) {
                criteria.andLessThanOrEqualTo("price", request.getPriceLte());
            }
            criteria.andBetween("price", request.getPriceGt(), request.getPriceLte());
            if (request.getSort().equals("1")) {
                example.setOrderByClause("price");
            } else if (request.getSort().equals("-1")) {
                example.setOrderByClause("price desc");
            }
            List<Item> items = itemMapper.selectByExample(example);

            PageHelper.startPage((int) Math.ceil((double) items.size() / request.getSize()), request.getSize());
            List<Item> itemList;
            if (request.getPage() * request.getSize() < items.size()) {
                 itemList = items.subList(request.getSize() * (request.getPage() - 1), request.getPage() * request.getSize());
            }else {
                 itemList = items.subList(request.getSize() * (request.getPage() - 1), items.size());

            }

            List<ProductDto> productDtos = productConverter.items2Dto(itemList);
            response.setData(productDtos);
            response.setTotal((long) items.size());
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("com.mall.shopping.services.IProductServiceImpl Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    /**
     * lxy
     *
     * @return
     */
    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();
        try {
            PanelDto panelDto = panelMapper.selectRecommend();
            Integer id = panelDto.getId();
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(id);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);
            HashSet<PanelDto> panelDtos = new HashSet<>();
            panelDtos.add(panelDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setPanelContentItemDtos(panelDtos);
        } catch (Exception e) {
            log.error("com.mall.shopping.services.IProductServiceImpl occur Exception: " + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
